package controller

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// User 定义用户结构体
type User struct {
	ID           int    `json:"id"`
	Name         string `json:"name"`
	Username     string `json:"username"`
	Mobile       string `json:"mobile"`
	Status       int    `json:"status"`
	TypeTxt      string `json:"type_txt"`
	RoleTxt      string `json:"role_txt"`
	OperatorName string `json:"operator_name"`
	LoginTime    string `json:"login_time"`
	CreateTime   string `json:"create_time"`
	UpdateTime   string `json:"update_time"`
}

// Option 定义选项结构体
type Option struct {
	Value interface{} `json:"value"`
	Label string      `json:"label"`
}

// GetBackUserList 获取后台用户列表
func GetBackUserList(c *gin.Context) {
	// 获取分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "100"))

	// 模拟用户列表
	var userList []User
	for i := 0; i < limit; i++ {
		index := (page-1)*limit + i
		userList = append(userList, User{
			ID:           index,
			Name:         "用户" + strconv.Itoa(index),
			Username:     "user" + strconv.Itoa(index),
			Mobile:       "138000000" + strconv.Itoa(index%100),
			Status:       index % 2,
			TypeTxt:      map[int]string{0: "普通用户", 1: "管理员"}[index%2],
			RoleTxt:      map[int]string{0: "查看", 1: "编辑,查看"}[index%2],
			OperatorName: "Admin",
			LoginTime:    "2024-04-01 10:00:00",
			CreateTime:   "2024-01-01 10:00:00",
			UpdateTime:   "2024-04-01 10:00:00",
		})
	}

	// 用户类型选项
	typeOptions := []Option{
		{Value: 0, Label: "普通用户"},
		{Value: 1, Label: "管理员"},
	}

	// 权限选项
	roleOptions := []Option{
		{Value: "view", Label: "查看"},
		{Value: "edit", Label: "编辑"},
		{Value: "admin", Label: "管理员"},
	}

	// 返回统一格式响应
	response := NewResponse(Success, gin.H{
		"list":  userList,
		"total": 50,
		"type":  typeOptions,
		"role":  roleOptions,
	})

	// 返回JSON响应
	c.JSON(http.StatusOK, response)
}
